package com.zyj.rabbitmq.send;

import com.zyj.rabbitmq.config.RabbitConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class Sender {

    @Autowired
    private AmqpTemplate amqpTemplate;


    public void send() {
        String context = "hello " + new Date();
        System.out.println("Sender : " + context);
        this.amqpTemplate.convertAndSend("hello", context);
    }

    public void p2pSend(){
        String context = "p2pQueue " + new Date();
        System.out.println("Sender : " + context);
        this.amqpTemplate.convertAndSend("p2pQueue", context);
    }

    public void sendFanout(Object message){
        System.out.println("Sender : " + message);
        this.amqpTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,"",message);
    }

    public void sendLazy(Object msg) {
        System.out.println("begin send lazy message<" + msg + ">");
        amqpTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME_LAZY, RabbitConfig.EXCHANGE_NAME_LAZY, msg, message -> {
            message.getMessageProperties().setHeader("x-delay", 10000);
            return message;
        });
    }
}
